/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.payment.handler;

import net.martinimix.domain.payment.Account;
import net.martinimix.domain.payment.Payment;
import net.martinimix.domain.payment.PaymentMethod;
import net.martinimix.service.payment.AbstractAccountPaymentService;

/**
 * Provides an account payment handler.
 * 
 * @author Scott Rossillo
 *
 */
public abstract class AbstractAccountPaymentHandler extends AbstractAccountPaymentService implements PaymentHandler {

	private int order;
	
	/**
	 * Creates a new account payment handler.
	 */
	public AbstractAccountPaymentHandler() { }
	
	/**
	 * Returns true if the given payment method has not yet been authorized.
	 * 
	 * @see net.martinimix.service.payment.handler.PaymentHandler#authorizationRequired(net.martinimix.domain.payment.Payment, net.martinimix.domain.payment.PaymentMethod)
	 */
	public boolean authorizationRequired(Payment payment, PaymentMethod paymentMethod) {
		return !Payment.STATUS_AUTHORIZED.equals(payment.getStatusCode());
	}
		
	/**
	 * Returns true if the given payment method can be cast
	 * to an <code>Account</code>.
	 * 
	 * @see net.martinimix.service.payment.handler.PaymentHandler#supports(net.martinimix.domain.payment.PaymentMethod)
	 */
	public boolean supports(PaymentMethod paymentMethod) {
		
		if(paymentMethod == null) {
			return false;
		}
		
		return Account.class.isAssignableFrom(paymentMethod.getClass());
	}
	

	/* (non-Javadoc)
	 * @see org.springframework.core.Ordered#getOrder()
	 */
	public final int getOrder() {
		return order;
	}
	
	/**
	 * Sets the order of this payment handler.
	 * 
	 * @param order the order of this <code>PaymentHandler</code>
	 */
	public final void setOrder(int order) {
		this.order = order;
	}
	
}
